package app 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.printing.PrintJob;
	import flash.printing.PrintJobOrientation;
	import utils.Logger;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class PrintPage 
	{
		
		public function PrintPage() 
		{
			
		}
		
		public static function print(page:Sprite):void
		{
			trace("start print");
			 var pj:PrintJob = new PrintJob();
            var pagesToPrint:uint = 0;
            if (pj.start()) 
			{                
               /* if (pj.orientation == PrintJobOrientation.PORTRAIT) 
				{
                    throw new Error("Without embedding fonts you must print two sheets per page with an orientation of landscape.");
                }
                */
				//scale
				var scale:Number = 1;
				if (page.width > pj.pageWidth)
				{
					scale = pj.pageWidth / page.width;
					page.scaleX = page.scaleY = scale;
					log("scale width: " + scale);
				}
				
                if (page.height > pj.pageHeight)
				{
					scale = scale * pj.pageHeight / page.height;
					page.scaleX = page.scaleY = scale;
					log("scale pageHeight: " + scale);
				}
				
                try 
				{
                    pj.addPage(page);
                    pagesToPrint++;
                }
                catch (e:Error) 
				{
                    // do nothing
                }

                if (pagesToPrint > 0) 
				{
                    pj.send();
                }
            }
		}
		
		private static function log(msg:String):void
		{
			Logger.log(null, "Print: " + msg);
		}
		
	}

}